<?php
class istance_data extends data 
{
	public function __construct()
	{
		parent::__construct();
		
	}

	
	
	public function getAllIstanceList()
	{
		$this->query("SELECT i.istanceid, i.istancename, i.istanceshortname, i.istancestatus, count(b.bossname) as bosscount, SUM(IF(b.bossstatus='Ucciso',1,0)) as countbosskill FROM
		istance i LEFT OUTER JOIN boss b on (i.istanceid=b.istanceid) group by i.istanceid, i.istancename
		");
		
		if($this->num_rows())
		{
			while(list($istanceid, $istancename, $istanceshortname, $istancestatus, $countboss, $countbosskill) = $this->fetch_row())
			{
				$array['id'] = $istanceid;
				$array['istancename'] = $istancename;
				$array['istanceshortname'] = $istanceshortname;
				$array['istancestatus'] = $istancestatus;
				$array['bosscount'] = $countboss."/".$countbosskill;
				$returndata[] = $array;
			}
			return $returndata;
		}
		else 
			return null;
	}

	public function setIstance()
	{
		if(isset($_POST['istanceid']) and $_POST['istanceid'] != null)
		{
			if(!$this->query("UPDATE istance SET 
				istancename = '{$_POST['istancename']}', 
				istanceshortname = '{$_POST['istanceshortname']}', 
				istanceimage = '{$_POST['istanceimage']}',
				istanceicon = '{$_POST['istanceicon']}',
				istancestatus = '{$_POST['istancestatus']}',
				istancedesc = '{$_POST['istancedesc']}'
				WHERE istanceid = '{$_POST['istanceid']}'"))
				return $this->error;
		}
		else 
		{
			if(!$this->query("INSERT INTO istance SET 
				istancename = '{$_POST['istancename']}', 
				istanceshortname = '{$_POST['istanceshortname']}', 
				istanceimage = '{$_POST['istanceimage']}',
				istanceicon = '{$_POST['istanceicon']}',
				istancestatus = '{$_POST['istancestatus']}',
				istancedesc = '{$_POST['istancedesc']}'"))				
				return $this->error;
		}
		
	}
	
	public function deleteIstance()
	{
		if(!$this->query("DELETE FROM istance WHERE istanceid = '{$_POST['istanceid']}'"))
			return $this->error;
	}
	
	public function getIstance()
	{
		$this->query("SELECT * FROM istance WHERE istanceid = '{$_POST['istanceid']}'");
		return $this->fetch_array();
	}
}
?>